﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using T4Template.Dal;
using T4Template.Dto;
using T4Template.Entities;

namespace T4Template.Bl
{
    public class CodeTemplateBL
    {
         private CodeTemplateDal dal;

        public CodeTemplateBL()
        {
            dal = CodeTemplateDal.GetInstance();
        }

        public ActionResultDto<CodeTemplateDto> Create(CreateCodeTemplateDto dto)
        {
            ActionResultDto<CodeTemplateDto> result = new ActionResultDto<CodeTemplateDto>();
            result.Data = null;

            try
            {
                CodeTemplate entity = new CodeTemplate();
                //entity.Id = ++count;
                entity.PatternType = dto.PatternType;
                entity.Name = dto.Name;
                entity.CodeType = dto.CodeType;
                entity.Active = true;
                entity.CreateDate = DateTime.Now;
                entity.Comment = dto.Comment;
                entity.TemplateNameSpaceList = dto.TemplateNameSpaceList;
                entity.TemplateClassNameList = dto.TemplateClassNameList;
                entity.Version = 1;
                entity.Revision = 0;
                dal.Add(entity);

                result.Status = true;
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.ErrorMessage = ex.Message;
            }

            return result;
        }

        public ActionResultDto<CodeTemplateDto> Get(GetCodeTemplateDto dto)
        {
            ActionResultDto<CodeTemplateDto> result = new ActionResultDto<CodeTemplateDto>();
            
            try
            {
                SearchCriteria criteria = new SearchCriteria();
                criteria.CodeType = dto.CodeType;
                criteria.PatternType = dto.PatternType;
                CodeTemplate foundTemplate = dal.Get(criteria);
                
                CodeTemplateDto data = new CodeTemplateDto();
                data.Id = foundTemplate.Id;
                data.PatternType = foundTemplate.PatternType;
                data.Name = foundTemplate.Name;
                data.CodeType = foundTemplate.CodeType;
                data.Active = foundTemplate.Active;
                data.CreateDate = foundTemplate.CreateDate;
                data.Comment = foundTemplate.Comment;
                data.TemplateNameSpaceList = foundTemplate.TemplateNameSpaceList;
                data.TemplateClassNameList = foundTemplate.TemplateClassNameList;
                data.Version = foundTemplate.Version;
                data.Revision = foundTemplate.Revision;

                result.Data = data;
                result.Status = true;
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Status = false;
                result.ErrorMessage = ex.Message;
            }

            return result;
        }
    }
}
